#!/bin/bash

# ============================================
# Mall 微服务项目 - 订单流程测试脚本
# ============================================

echo "=========================================="
echo "Mall 微服务订单流程测试"
echo "=========================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 检查服务是否启动
echo -e "${YELLOW}1. 检查服务状态...${NC}"
echo ""

# 检查订单服务
echo -n "检查订单服务 (8081)... "
if curl -s http://localhost:8081/api/orders/health > /dev/null 2>&1; then
    echo -e "${GREEN}✓ 运行中${NC}"
else
    echo -e "${RED}✗ 未启动${NC}"
    echo "请先启动 order-service"
    exit 1
fi

# 检查库存服务
echo -n "检查库存服务 (8082)... "
if nc -z localhost 8082 2>/dev/null; then
    echo -e "${GREEN}✓ 运行中${NC}"
else
    echo -e "${RED}✗ 未启动${NC}"
    echo "请先启动 inventory-service"
    exit 1
fi

# 检查支付服务
echo -n "检查支付服务 (8083)... "
if nc -z localhost 8083 2>/dev/null; then
    echo -e "${GREEN}✓ 运行中${NC}"
else
    echo -e "${RED}✗ 未启动${NC}"
    echo "请先启动 payment-service"
    exit 1
fi

echo ""
echo -e "${YELLOW}2. 创建测试订单...${NC}"
echo ""

# 创建订单
RESPONSE=$(curl -s -X POST http://localhost:8081/api/orders \
  -H "Content-Type: application/json" \
  -d '{
    "userId": 1001,
    "shippingAddress": "北京市朝阳区xxx街道xxx号",
    "remark": "测试订单 - 请尽快发货",
    "items": [
      {
        "skuId": 1,
        "skuName": "iPhone 15 Pro 256GB 深空黑色",
        "price": "7999.00",
        "quantity": 1
      },
      {
        "skuId": 3,
        "skuName": "AirPods Pro 2 主动降噪",
        "price": "1899.00",
        "quantity": 2
      }
    ]
  }')

# 提取订单编号
ORDER_NO=$(echo $RESPONSE | grep -o '"orderNo":"[^"]*' | cut -d'"' -f4)

if [ -z "$ORDER_NO" ]; then
    echo -e "${RED}✗ 订单创建失败${NC}"
    echo "响应: $RESPONSE"
    exit 1
fi

echo -e "${GREEN}✓ 订单创建成功${NC}"
echo "订单编号: $ORDER_NO"
echo ""

# 显示订单详情
echo -e "${YELLOW}订单详情:${NC}"
echo "$RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$RESPONSE"
echo ""

echo -e "${YELLOW}3. 等待订单处理...${NC}"
echo ""
echo "订单处理流程:"
echo "  1. order-service 创建订单 → 发送 order_created 事件"
echo "  2. inventory-service 预留库存 → 发送 order_stock_reserved 事件"
echo "  3. payment-service 模拟支付 → 发送 order_paid 事件"
echo "  4. order-service 更新订单状态为已支付"
echo ""

# 等待订单处理完成（最多等待 10 秒）
MAX_WAIT=10
WAIT_COUNT=0

while [ $WAIT_COUNT -lt $MAX_WAIT ]; do
    sleep 1
    WAIT_COUNT=$((WAIT_COUNT + 1))
    
    # 查询订单状态
    ORDER_STATUS=$(curl -s http://localhost:8081/api/orders/$ORDER_NO | grep -o '"status":"[^"]*' | cut -d'"' -f4)
    
    echo -n "."
    
    if [ "$ORDER_STATUS" = "PAID" ]; then
        echo ""
        echo -e "${GREEN}✓ 订单处理完成！${NC}"
        break
    fi
done

echo ""
echo ""

# 查询最终订单状态
echo -e "${YELLOW}4. 查询订单最终状态...${NC}"
echo ""

FINAL_ORDER=$(curl -s http://localhost:8081/api/orders/$ORDER_NO)
FINAL_STATUS=$(echo $FINAL_ORDER | grep -o '"status":"[^"]*' | cut -d'"' -f4)

echo "订单编号: $ORDER_NO"
echo "订单状态: $FINAL_STATUS"
echo ""

if [ "$FINAL_STATUS" = "PAID" ]; then
    echo -e "${GREEN}=========================================="
    echo "✓ 测试成功！订单已完成支付"
    echo "==========================================${NC}"
else
    echo -e "${YELLOW}=========================================="
    echo "订单状态: $FINAL_STATUS"
    echo "可能还在处理中，请稍后查询"
    echo "==========================================${NC}"
fi

echo ""
echo "完整订单信息:"
echo "$FINAL_ORDER" | python3 -m json.tool 2>/dev/null || echo "$FINAL_ORDER"
echo ""

echo -e "${YELLOW}提示:${NC}"
echo "- 可以查看各服务的日志了解详细处理过程"
echo "- 可以访问 AKHQ (http://localhost:9001) 查看 Kafka 消息"
echo "- 可以查询数据库验证数据变化"
echo ""

